/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.dialog.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.genia.toolbox.basics.editor.gui.dialog.panel.AbstractSettingsPanel;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.gui.view.internal.renderer.ComboBoxRenderer;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.form.editor.model.bean.FormInitialSettings;
import com.genia.toolbox.web.gwt.form.server.description.item.ItemDescription;
import com.genia.toolbox.web.gwt.form.server.description.item.impl.ItemDescriptionImpl;

/**
 * The form selection panel. Used to create a form or to select a sub-form.
 */
@SuppressWarnings("serial")
public class ItemSelectionPanel
    extends AbstractSettingsPanel<FormEditorGUI, FormInitialSettings>
{

  /**
   * The element name label.
   */
  private JLabel labelName = null;

  /**
   * The element name textfield.
   */
  private JTextField textName = null;

  /**
   * The label of the form type.
   */
  private JLabel labelType = null;

  /**
   * The selection of the form type.
   */
  private JComboBox comboType = null;

  /**
   * The label of the description.
   */
  private JLabel labelDescription = null;

  /**
   * The selection of the description.
   */
  private JTextArea areaDescription = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   */
  public ItemSelectionPanel(FormEditorGUI gui)
  {
    super(gui);

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    this.setLayout(layout);

    this.labelName = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_DIALOG_CREATION_LABEL_NAME));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.insets = new Insets(5, 5, 5, 5);
    this.add(this.labelName, constraints);

    this.textName = new JTextField();
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 0, 5, 5);
    this.add(this.textName, constraints);

    this.labelType = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_DIALOG_CREATION_LABEL_TYPE));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.insets = new Insets(0, 5, 5, 5);
    this.add(this.labelType, constraints);

    this.comboType = new JComboBox(this.getItemsModel());
    this.comboType.addActionListener(this);
    this.comboType.setRenderer(new ComboBoxRenderer(getGui()));
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 5, 5);
    this.add(this.comboType, constraints);

    this.labelDescription = new JLabel(this.getGui().getController().notifyTranslation(Messages.ITEM_DIALOG_CREATION_LABEL_DESCRIPTION));
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.insets = new Insets(0, 5, 5, 5);
    this.add(this.labelDescription, constraints);

    this.areaDescription = new JTextArea();
    this.areaDescription.setLineWrap(true);
    this.areaDescription.setWrapStyleWord(true);
    this.areaDescription.setFont(this.labelDescription.getFont());
    this.areaDescription.setEditable(false);
    constraints.gridx = 2;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 0, 5, 5);
    this.add(this.areaDescription, constraints);

    this.comboType.setSelectedIndex(0);

  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source.equals(this.comboType)) {
      ItemDescription description = (ItemDescription) this.comboType.getSelectedItem();
      if (description != null) {
        this.areaDescription.setText(this.getGui().getController().notifyTranslation(description.getDescription()));
      }
    }
  }



  /**
   * Get the settings from the current selection.
   * 
   * @return the settings.
   */
  @Override
  public FormInitialSettings getSettings()
  {
    FormInitialSettings settings = null;
    ItemDescriptionImpl itemDescription = (ItemDescriptionImpl) this.comboType.getSelectedItem();
    if (itemDescription != null) {
      settings = new FormInitialSettings(itemDescription);
      settings.setDocumentName(this.textName.getText());
    }
    return settings;
  }



  /**
   * Create a model containing all the items.
   * 
   * @return a model containing all the items.
   */
  public ComboBoxModel getItemsModel()
  {
    ComboBoxModel model = new DefaultComboBoxModel(this.getGui().getController().notifyDisplayItemDescriptions().toArray());
    return model;
  }

}
